define([ "dojo/dom", "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/graphic", "esri/geometry/Point",
	"esri/tasks/query", "esri/symbols/SimpleMarkerSymbol", "esri/SpatialReference", "esri/layers/ArcGISImageServiceLayer", "esri/layers/WMSLayer", "esri/layers/WMTSLayer",
	"util/config/constant", "util/wechat/wechat", "util/config/config", "util/map/toolbar/graphic", "esri/layers/GraphicsLayer" ],
	function(dom, ArcGISTiledMapServiceLayer, ArcGISDynamicMapServiceLayer, Graphic, Point,
		Query, SimpleMarkerSymbol, SpatialReference, ArcGISImageServiceLayer, WMSLayer, WMTSLayer, constantUtil, wechatUtil, configUtil, graphicUtil, GraphicsLayer) {
		return {
			getLayer : function(layerId, layerServiceType, layerUrl, layerOpacity) {
				var options = {};
				if (layerId != null) {
					options.id = layerId;
					options.opacity = layerOpacity;
				}
				var layer = null;
				switch (layerServiceType) {
				case 0:
					layer = new ArcGISTiledMapServiceLayer(layerUrl, options);
					break;
				case 1:
					layer = new ArcGISDynamicMapServiceLayer(layerUrl, options);
					break;
				case 2:
					layer = new GraphicsLayer({id : layerId, layerOpacity});
					break;
				default:
					throw new Error("不支持的地图服务，serviceType=" + layerServiceType);
				}
				return layer;
			},
			loadKssLayer : function(map, layer) {
				$.ajax({
					url : configUtil.getAjaxUrl('/project/listYkssPopulation.do'),
					type : "post",
					dataType : "text",
					data : {
						'openid' : configUtil.getOpenid(),
					},
					success : function(data) {
						layer.clear();
						map.graphics.clear();
						var queryTask = new esri.tasks.QueryTask(constantUtil.LYXB_URL + "/0");
						var query = new Query();
						query.outFields = [ "*" ];
						query.where = "fid in (" + data + ")";
						query.returnGeometry = true;
						queryTask.execute(query, function(featureSet) {
							var resultFeatures = featureSet.features;
							var mys = [ 255, 48, 48, 0.3 ];
							var xys = [ 255, 0, 0 ];
							graphicUtil.addGraToMap(map, resultFeatures, mys, xys, layer);
						});
					}
				});
			},
			loadCzsLayer : function(map, layer) {
				$.ajax({
					url : configUtil.getAjaxUrl('/project/listYczs.do'),
					type : "post",
					dataType : "json",
					data : {
						'openid' : configUtil.getOpenid()
					},
					success : function(data) {
						layer.clear();
						map.graphics.clear();
						for (var i = 0; i < data.length; i++) {
							var point = null;
							if (data[i].pointX != null) {
								point = new Point(data[i].pointX, data[i].pointX, new SpatialReference({
									wkid : 3857
								}));
							} else {
								point = wechatUtil.getGdLocaltion(data[i].longitude, data[i].latitude);
							}
							var simpleMarkerSymbol = new SimpleMarkerSymbol();
							var symbol = new esri.symbol.PictureMarkerSymbol(
								configUtil.getAjaxUrl("/img/map/icon/tree.png"), 16, 18);
							var graphic = new Graphic(point, simpleMarkerSymbol);
							graphic.setSymbol(symbol);
							layer.add(graphic);
						}
					}
				});
				$.ajax({
					url : configUtil.getAjaxUrl('/project/listYczm.do'),
					type : "post",
					dataType : "text",
					data : {
						'openid' : configUtil.getOpenid()
					},
					success : function(data) {
						var queryTask = new esri.tasks.QueryTask(constantUtil.YGYX_URL + "/0");
						var query = new Query();
						query.outFields = [ "*" ];
						query.where = "fid in (" + data + ")";
						query.returnGeometry = true;
						queryTask.execute(query, function(featureSet) {
							var resultFeatures = featureSet.features;
							var mys = [ 0, 245, 255, 0.3 ];
							var xys = [ 0, 245, 255 ];
							graphicUtil.addGraToMap(map, resultFeatures, mys, xys, layer);
						});
					}
				});
			},
			addPlane : function(map, layer, condition, mys, xys) {
				var queryTask = new esri.tasks.QueryTask(constantUtil.YGYX_URL + "/0");
				var query = new Query();
				query.outFields = [ "*" ];
				query.where = condition;
				query.returnGeometry = true;
				queryTask.execute(query, function(featureSet) {
					var resultFeatures = featureSet.features;
					graphicUtil.addGraToMap(map, resultFeatures, mys, xys, layer);
				});
			},
			addPoint : function(layer, point, url) {
				var simpleMarkerSymbol = new SimpleMarkerSymbol();
				var symbol = new esri.symbol.PictureMarkerSymbol(configUtil.getAjaxUrl(url), 25, 32);
				var graphic = new Graphic(point, simpleMarkerSymbol);
				graphic.setSymbol(symbol);
				layer.add(graphic);
			}
		};
	});